/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package utopiaintelpackage.som;

import java.util.Calendar;

import main.CommonMethods;
import main.Constants;
import botobjects.SoMArmy;
import exceptions.IntelParsingException;

/**
 * This class parses SoMs
 * 
 * 
 */
public class SoMParser {
   private SoMArmy army;
   private final String persregex = CommonMethods.getPersonalitiesRegex();
   private final String titlesregex = CommonMethods.getTitlesRegex();
   private final String racesregex = CommonMethods.getRacesRegex(false);
   private final String dspecsregex = CommonMethods.getDspecsRegex();
   private final String ospecsregex = CommonMethods.getOspecsRegex();
   private final String elitesregex = CommonMethods.getElitesRegex();

   public void parseFormattedSoM(final String input, final SoM som) throws IntelParsingException {
	  String in = input;
	  if (in.matches("Military Intelligence on .*\\([0-9]{1,2}(:)[0-9]{1,2}\\)")) {
		 in = in.replace("Military Intelligence on ", "");

		 String prov = in.substring(0, in.indexOf('(')).trim();
		 String kd = (in.substring(in.indexOf('('), in.indexOf(')')) + ")").trim();
		 som.setName(prov);
		 som.setKdloc(kd);
	  } else if (in.matches("Military Intel on .*\\([0-9]{1,2}(:)[0-9]{1,2}\\)")) {
		 in = in.replace("Military Intel on ", "");

		 String prov = in.substring(0, in.indexOf('(')).trim();
		 String kd = (in.substring(in.indexOf('('), in.indexOf(')')) + ")").trim();
		 som.setName(prov);
		 som.setKdloc(kd);
	  } else if (in.matches("Ruler Name: The " + persregex + " " + titlesregex + " .*")) {
		 String title = in.replaceAll("Ruler Name: The " + persregex + " ", "");
		 String rulerName = title.replaceFirst(titlesregex, "").trim();
		 title = title.replace(rulerName, "").trim();
		 som.setRulerName(rulerName);
		 som.setTitle(title);
	  } else if (in.matches("Ruler Name: The " + persregex + " .*")) {
		 String rulerName = in.replaceAll("Ruler Name: The " + persregex + " ", "");
		 som.setRulerName(rulerName);
		 som.setTitle("Peasant");
	  } else if (in.matches("Ruler Name: " + titlesregex + " .* the " + persregex)) {
		 String rulerName = in.replaceAll("Ruler Name: " + titlesregex + " ", "");
		 rulerName = rulerName.replaceAll(" the " + persregex, "");
		 String title = in.replace("Ruler Name: ", "");
		 title = title.replaceAll(rulerName + ".*", "");
		 som.setRulerName(rulerName);
		 som.setTitle(title);
	  } else if (in.matches("Ruler Name: .* the " + persregex)) {
		 String rulerName = in.replaceAll("Ruler Name: ", "");
		 rulerName = rulerName.replaceAll(" the " + persregex, "");
		 som.setRulerName(rulerName);
		 som.setTitle("Peasant");
	  } else if (in.matches("Personality & Race: The " + persregex + ", " + racesregex)) {
		 in = in.replace("Personality & Race: The ", "");
		 String pers = in.substring(0, in.indexOf(',')).trim();
		 String race = in.substring(in.indexOf(',') + 1).trim();
		 som.setPersonality(pers);
		 som.setRace(race);
	  } else if (in.matches("Net Defense at Home \\(from Utopia\\): [0-9,]{1,}")) {
		 som.setNetDefense(Integer.parseInt(in.replaceAll("[^0-9]", "")));
	  } else if (in.matches("Net Offense at Home \\(from Utopia\\): [0-9,]{1,}")) {
		 som.setNetOffense(Integer.parseInt(in.replaceAll("[^0-9]", "")));
	  } else if (in.equals("** Standing Army (At Home) **")) {
		 if (army == null) {
			army = new SoMArmy(som.getRace(), 0L, 0, Constants.SOM_ARMY_HOME);
		 }
	  } else if (in.matches("\\*\\* Army #[2-7] \\(Back in [0-9]{1,2}(:)[0-9]{1,2} hours\\) \\*\\*")) {
		 int armyno = Integer.parseInt(in.substring(in.indexOf('A'), in.indexOf('(')).replaceAll("[^2-7]", ""));
		 String time = in.substring(in.indexOf('('), in.indexOf(')')).replaceAll("[^0-9:]", "");
		 String[] split = time.split(":");
		 Long returnTime = Calendar.getInstance().getTimeInMillis() + Integer.parseInt(split[0]) * 3600000L
			   + (Integer.parseInt(split[1]) - 5) * 60000L;

		 if (army == null) {
			army = new SoMArmy(som.getRace(), returnTime, armyno, Constants.SOM_ARMY_OUT);
		 } else {
			som.addArmy(new SoMArmy(army));
			if (returnTime <= Calendar.getInstance().getTimeInMillis()) {
			   army = null;
			} else {
			   army = new SoMArmy(som.getRace(), returnTime, armyno, Constants.SOM_ARMY_OUT);
			}
		 }
	  } else if (in.matches("Generals: [1-5] .*")) {
		 if (army != null) {
			army.setGenerals(Integer.parseInt(in.split(" ")[1]));
		 }
	  } else if (in.matches("Soldiers: [0-9,]{1,}.*")) {
		 if (army != null) {
			army.setSoldiers(Integer.parseInt(in.split(" ")[1].replaceAll("[^0-9]", "")));
		 }
	  } else if (in.matches(dspecsregex + ": [0-9,]{1,}.*")) {
		 String temp = in.replaceFirst(dspecsregex, "");
		 if (army != null) {
			army.setDspecs(Integer.parseInt(temp.split(" ")[1].replaceAll("[^0-9]", "")));
		 }
	  } else if (in.matches(CommonMethods.getOspecsRegex() + ": [0-9,]{1,}.*")) {
		 String temp = in.replaceFirst(ospecsregex, "");
		 if (army != null) {
			army.setOspecs(Integer.parseInt(temp.split(" ")[1].replaceAll("[^0-9]", "")));
		 }
	  } else if (in.matches(CommonMethods.getElitesRegex() + ": [0-9,]{1,}.*")) {
		 String temp = in.replaceFirst(elitesregex, "");
		 if (army != null) {
			army.setElites(Integer.parseInt(temp.split(" ")[1].replaceAll("[^0-9]", "")));
		 }
	  } else if (in.matches("Thieves: [0-9,]{1,}")) {
		 if (army != null) {
			army.setThieves(Integer.parseInt(in.split(" ")[1].replaceAll("[^0-9]", "")));
		 }
	  } else if (in.matches("Captured Land: [0-9,]{1,} Acres")) {
		 if (army != null) {
			army.setLand(Integer.parseInt(in.replaceAll("[^0-9]", "")));
		 }
	  } else if ("** Troops in Training **".equals(in)) {
		 if (army == null) {
			army = new SoMArmy(som.getRace(), 0L, 0, Constants.SOM_ARMY_TRAINING);
		 } else {
			som.addArmy(new SoMArmy(army));
			army = new SoMArmy(som.getRace(), 0L, 0, Constants.SOM_ARMY_TRAINING);
		 }
	  } else if (in.equals("** Finished **") && army != null) {
		 som.addArmy(new SoMArmy(army));
		 army = null;
	  }
   }

   public void parseUnformattedSoM(final String in, final SoM som) throws IntelParsingException {
	  /*
	   * SoM som=new SoM(); ArrayList<ArmyOut> armiesout=new ArrayList<ArmyOut>(); armiesout.add(new ArmyOut()); armiesout.add(new
	   * ArmyOut()); armiesout.add(new ArmyOut()); armiesout.add(new ArmyOut()); for(String in:intel){
	   * if(in.matches("Military Affairs of [a-zA-Z0-9 ]{1,20}")){ in=in.replace("Military Affairs of ", ""); String
	   * prov=in.substring(0, in.indexOf('(')).trim(); som.setName(prov); } else
	   * if(in.matches(utoHandler.getTitlesRegex()+" .*")){ String title=in.substring(0,in.indexOf(' '));
	   * som.setTitle(utoHandler.getTitleMapping(title)); } else if(in.matches("At this time, approximately [0-9,.]{1,}%.*")){
	   * String estDraft=in.substring(0,in.indexOf('%')); estDraft=estDraft.replace("At this time, approximately ", "");
	   * som.setEstDraft(estDraft); } else if(in.matches("Net Offensive Points at Home\t[0-9,]{1,}")){ String
	   * mo=in.replaceAll("[^0-9]", ""); som.setNetOffHome(Integer.parseInt(mo)); } else
	   * if(in.matches("Net Defensive Points at Home\t[0-9,]{1,}")){ String md=in.replaceAll("[^0-9]", "");
	   * som.setNetDefHome(Integer.parseInt(md)); } else if(in.matches("Soldiers\t[0-9,]{1,}.*")){ String[] temp=in.split("\t");
	   * som.setSoldiersHome(Integer.parseInt(temp[1].replace(",", "").trim())); for(int i=2;i<temp.length;++i){
	   * armiesout.get(i-2).setSoldiers(Integer.parseInt(temp[i].replace(",", "").trim())); } } else
	   * if(in.matches(utoHandler.getOspecsRegex()+"\t[0-9,]{1,}.*")){ String[] temp=in.split("\t");
	   * som.setOspecsHome(Integer.parseInt(temp[1].replace(",", "").trim())); for(int i=2;i<temp.length;++i){
	   * armiesout.get(i-2).setOspecs(Integer.parseInt(temp[i].replace(",", "").trim())); } } else
	   * if(in.matches(utoHandler.getDspecsRegex()+"\t[0-9,]{1,}.*")){ String[] temp=in.split("\t");
	   * som.setDspecsHome(Integer.parseInt(temp[1].replace(",", "").trim())); } else
	   * if(in.matches(utoHandler.getElitesRegex()+"\t[0-9,]{1,}.*")){ String[] temp=in.split("\t");
	   * som.setElitesHome(Integer.parseInt(temp[1].replace(",", "").trim())); for(int i=2;i<temp.length;++i){
	   * armiesout.get(i-2).setElites(Integer.parseInt(temp[i].replace(",", "").trim())); } } else
	   * if(in.matches("War Horses\t[0-9,]{1,}.*")){ String[] temp=in.split("\t");
	   * som.setHorsesHome(Integer.parseInt(temp[1].replace(",", "").trim())); for(int i=2;i<temp.length;++i){
	   * armiesout.get(i-2).setHorses(Integer.parseInt(temp[i].replace(",", "").trim())); } } else
	   * if(in.matches("Captured Land\t(-).*")){ String[] temp=in.split("\t"); for(int i=2;i<temp.length;++i){
	   * armiesout.get(i-2).setGains(Integer.parseInt(temp[i].replace(",", "").trim())); } } } som.setArmiesOut(armiesout); new
	   * SoMtoDB(con).saveSoM(som);
	   */
   }
}
